#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "main.h"
#include "server.h"
#include "client.h"


/**  Server Variables */

/**  Client Variables */
int isConnected;
char* server_ipaddress;

int main(const int argc, const char *argv[]){

    if(argc < 2){
        fprintf(stderr,"Error no program specified\n");
        return -1;
    }
    
    if(strcmp(argv[1],_SERVER_) == 0){
        // This is a server program
        printf("Initializing the server program...\n");
        initServer();
    }
    else if((strcmp(argv[1],_CLIENT_) == 0) && (argv[2] != 0)){
        // This is a client program
        printf("Initializing the client program...\n");
        int success = 1;

        server_ipaddress = strdup(argv[2]);
		printf("Server address: %s\n",server_ipaddress);
        client_connect(server_ipaddress);
        
        
        if(!(success < 0)){
            menu();
            client_disconnect(argv[2]);
        }
        else{
            fprintf(stderr,"\nError connecting to the server %s \n"
            "check the ip address and retry connection", argv[2]);
            return -1;
        }
    }
    else{
        printf("The correct format was not chosen!\n");
        printf("Hint: Be sure to include IP address when initiaizing a client");
        return -1;
    }
    
    return 0;
}


/** Client  interface functions *******************************************************************************/

int client_connect(const char* server_address){
    return connectClient(server_address);
    
}

int client_disconnect(const char* server_address){
    free(server_ipaddress);
}

int menu(){
    int repeat = 1;
    int choice = 4;
    printf("\n\nWelcome to the Banking Menu!\n");
    
    while(repeat){
        choice = 4;
        printf("\nChoose an option:\n");
        int i;
        for(i = 0; i < 4; ++i){
            printf("\t%s\n",MENU_OPTIONS[i]);
        }

        printf(">");
        scanf("%d",&choice);

        switch(choice){
            case 1:
                printf("You chose 1, Deposit\n");
                client_deposit();
                break;
            case 2:
                printf("You chose 2, Withdraw\n");
                client_withdraw();
                break;
            case 3:
                printf("You chose 3, Balance\n");
                client_balance();
                break;
            case 4:
                printf("You chose 4, Quit\n");
                repeat = 0;
                printf("Goodbye!\n");
                break;
            default:
                printf("You chose %d, this is invalid!\n",choice);
        }
    }

}

void client_deposit(){
    long int deposit_amount = 0l;
    struct atm_msg msg,rec_msg;
    int current_balance = -1, account_number = -1;
    printf("What is your account number?\n>");
    scanf("%d",&account_number);
    sprintf(msg.account_number, "%d",account_number);
    
    printf("How much would you like to deposit?\n>");
    scanf("%ld",&deposit_amount);
    //printf("Depositing %d into account number %d...");
    sprintf(msg.amount , "%ld",deposit_amount);

    msg.command = DEPOSIT;
    
    
    rec_msg = deposit(msg);
    
    if(rec_msg.issuccess != SUCCESS){
    	fprintf(stderr,"Error depositing: %s\n",rec_msg.msg);
    }
    else{
    	printf("Hello %s, Your new balance is %s.\n %s\n",rec_msg.name,
    			rec_msg.amount,rec_msg.msg);

    }
}
void client_withdraw(){
	long int withdraw_amount = 0l;
    struct atm_msg msg,rec_msg;
    int current_balance = -1, account_number = -1;
    printf("What is your account number?\n>");
    scanf("%d",&account_number);
    sprintf(msg.account_number, "%d",account_number);
    
    printf("How much would you like to withdraw?\n>");
    scanf("%ld",&withdraw_amount);
    sprintf(msg.amount , "%ld",withdraw_amount);

    msg.command = WITHDRAW;
    
    
    rec_msg = deposit(msg);
    
    if(rec_msg.issuccess != SUCCESS){
    	fprintf(stderr,"Error withdrawing: %s\n",rec_msg.msg);
    }
    else{
    	printf("Hello %s, Your new balance is %s.\n %s\n",rec_msg.name,
    			rec_msg.amount,rec_msg.msg);

    }
}
void client_balance(){
    struct atm_msg msg,rec_msg;
    int current_balance = -1, account_number = -1;
    printf("What is your account number?\n>");
    scanf("%d",&account_number);
    
    sprintf(msg.account_number, "%d",account_number);
    msg.command = BALANCE;
   
    rec_msg = balance(msg);	/*  Retrieve the balance from server */
    
    if(rec_msg.issuccess != SUCCESS){
    	fprintf(stderr,"Error retrieving balance: %s\n",rec_msg.msg);
    }
    else{
    	printf("Hello %s, Your current balance is %s.\n %s\n",rec_msg.name,
    			rec_msg.amount,rec_msg.msg);
    }
}

/** Server  interface functions *******************************************************************************/

int initServer(){
    int success = -1;
    
    success = createServerSocket( );
    if(success < 0){
        perror("Error creating server socket\n");
        return -1;
    }
        
    success = bindServerSocket();
    if(success < 0){
        perror("Error binding server to socket \n");
        return -1;
    }
    
    
    success = setSocketToListen();
    if(success < 0){
        perror("Error setting socket to listen \n");
        return -1;
    }
    
    success = startServer();
    if(success < 0){
        perror("Error starting server \n");
        return -1;
    }
}
